package com.lt.service.impl;

import com.lt.mapper.AppInfoMapper;
import com.lt.mapper.AppVersionMapper;
import com.lt.pojo.AppInfo;
import com.lt.pojo.AppVersion;
import com.lt.service.AppInfoService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.List;

@Service
public class AppInfoServiceImpl implements AppInfoService {

    @Autowired
    AppInfoMapper appInfoMapper;

    @Autowired
    AppVersionMapper appVersionMapper;

    @Override
    public List<AppInfo> selectAllByCondation(String softwareName, Integer status, Integer categoryLevel1, Integer categoryLevel2, Integer categoryLevel3, Integer devId, Integer flatformId, Integer from, Integer pageSize) {
        return appInfoMapper.selectAllByCondation(softwareName, status, categoryLevel1, categoryLevel2, categoryLevel3,devId,flatformId, from, pageSize);
    }


    @Override
    public Integer selectTotalNumber() {
        return appInfoMapper.selectTotalNumber();
    }

    @Override
    public AppInfo getAppInfo(Integer id, String APKName) {
        return appInfoMapper.getAppInfo(id, APKName);
    }

    @Override
    public boolean add(AppInfo appInfo) {
        if( appInfoMapper.add(appInfo)>0){
            return true;
        }else {
            return false;
        }

    }

    @Override
    public boolean modify(AppInfo appInfo) {
        return appInfoMapper.modify(appInfo)>0?true:false;
    }

    @Override
    public AppInfo getAppInfoById(Integer id) {
        return appInfoMapper.getAppInfoById(id);
    }

    @Override
    public Integer getAppInfoCount(String softwareName, Integer status, Integer categoryLevel1, Integer categoryLevel2, Integer categoryLevel3, Integer flatformId, Integer devId) {
        return appInfoMapper.getAppInfoCount(softwareName,status,categoryLevel1,categoryLevel2,categoryLevel3,flatformId,devId);
    }

    @Override
    public int updateApp(AppInfo appInfo) {
        return appInfoMapper.update(appInfo);
    }

    @Override
    public Integer deleteAppInfById(Integer id) {
        int result =-1;
        //先删除app_Version表的数据,同时删除相关保存在服务器中的文件
        List<AppVersion> appVersionList = appVersionMapper.getAppVersionByvId(id);
        if (appVersionList.size()>0){
            for (AppVersion appVersion:appVersionList ) {
                if (StringUtils.isNullOrEmpty(appVersion.getApklocpath()) ){
                    File file=new File(appVersion.getApklocpath());
                    if (file.exists())
                        file.delete();
                }
            }
            //删除Version数据信息
            result=appVersionMapper.delAppVersionByAppId(id);
        }

        //再删除app_Info表的数据，同时删除相关保存在服务器中的文件
        AppInfo appInfo = appInfoMapper.getAppInfoById(id);
        if (appInfo!=null){
            if (StringUtils.isNullOrEmpty(appInfo.getLogolocpath()) ){
                File file=new File(appInfo.getLogolocpath());
                if (file.exists())
                    file.delete();
            }
        }
        //删除app_Info数据信息
        result=appInfoMapper.delAppInfoById(id);
        return result;
    }

    @Override
    public boolean appsysUpdateSaleStatusByAppId(AppInfo appInfo) {
        Long modifyBy = appInfo.getModifyby();//modifyby：更新者
        AppInfo app = appInfoMapper.getAppInfoById(Integer.valueOf(String.valueOf(appInfo.getId())));//通过id获取appinfo信息
        List<AppVersion> appVersionList = appVersionMapper.getAppVersionByvId(Integer.valueOf(String.valueOf(app.getId())));//通过AppId获取AppVersion信息
        if(null == app){
            return false;
        }else{
            app.setModifyby(modifyBy);
            app.setModifydate(new Date());
            //获取商品的状态
            if (app.getStatus() == 2) { //当状态为审核通过时，可以进行上架操作
                app.setStatus(4L);
                app.setOnsaledate(new Date());//上架日期
                //修改appInfo
                appInfoMapper.update(app);
                //修改所有对应version信息的状态
                if (appVersionList.size() > 0) {
                    for (AppVersion appVersion : appVersionList) {
                        appVersion.setModifyby(modifyBy);
                        appVersion.setModifydate(new Date());
                        appVersion.setPublishstatus(2L);//appversion状态调整为已发布
                        appVersionMapper.modify(appVersion);//修改appversion
                    }
                }
            } else if (app.getStatus() == 5) {
                app.setStatus(4L);
                app.setOnsaledate(new Date());
                appInfoMapper.update(app);
                appInfoMapper.update(app);//修改appInfo
                //修改所有对应version信息的状态
                if (appVersionList.size() > 0) {
                    for (AppVersion appVersion : appVersionList) {
                        appVersion.setModifyby(modifyBy);
                        appVersion.setModifydate(new Date());
                        appVersion.setPublishstatus(2L);//appversion状态调整为已发布
                        appVersionMapper.modify(appVersion);//修改appversion
                    }
                }
            } else if (app.getStatus() == 4) {//当状态为上架时，可以进行下架操作
                app.setStatus(5L);
                app.setOffsaledate(new Date());
                appInfoMapper.update(app);//修改appInfo
                //修改所有对应version信息的状态
                if (appVersionList.size() > 0) {
                    for (AppVersion appVersion : appVersionList) {
                        appVersion.setModifyby(modifyBy);
                        appVersion.setModifydate(new Date());
                        appVersionMapper.modify(appVersion);//修改appversion
                    }
                }
            } else {
                return false;
            }
        }
        return true;
    }

    @Override
    public int updateAppInfoStatus(Integer status, Integer id) {
        return appInfoMapper.updateAppInfoStatus(status,id);
    }
}
